package com.android.questions.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.appcompat.widget.SwitchCompat;

/**
 * Author : Z-JC
 * Date : 2021/3/10
 * Description :
 */
public class SeniorSwitch extends SwitchCompat {
    /**
     * 监听是否存在动作
     */
    private boolean IF_EVENT_MOVE = false;
    /**
     * 监听滑动时间
     */
    private long MOVE_TIME = -1;

    public SeniorSwitch(Context context) {
        super(context);
    }

    public SeniorSwitch(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SeniorSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (IF_EVENT_MOVE && ev.getAction() == MotionEvent.ACTION_UP) {
            MOVE_TIME = -1;
            IF_EVENT_MOVE = false;
            setChecked(!(ev.getX() <= getWidth() / 2f));
        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
            //松开
            IF_EVENT_MOVE = false;
            MOVE_TIME = -1;
        } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            //动作
            if (MOVE_TIME == -1) {
                MOVE_TIME = System.currentTimeMillis();
            } else {
                if (System.currentTimeMillis() - MOVE_TIME > 500) {
                    IF_EVENT_MOVE = true;
                }
            }
        }
        return super.onTouchEvent(ev);
    }
}